package com.heimo.babyage;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.heimo.babyage.ui.dialog.AboutDialog;

public class BasicActivity extends Activity {
	private static final String TAG = "BasicActivity";
	protected int mAppWidgetId = -1;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.welcome, menu);
		return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		for(int i = 0 ; i<menu.size();i++){
			MenuItem item = menu.getItem(i);
			if(item.getItemId() == R.id.action_about){
				item.setVisible(true);
			}else{
				item.setVisible(false);
			}
			
		}
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int menuId, MenuItem item) {
		Log.v(TAG, "onMenuItemSelected : " + item.getItemId() + " menuId : "
				+ menuId);
		switch (item.getItemId()) {
			case R.id.action_settings:
				if (!(this instanceof PreferenceTextActivity)) {
					Intent myIntent = new Intent(this, PreferenceTextActivity.class);
					myIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
							mAppWidgetId);
					this.startActivity(myIntent);
				}
				return true;	
			case R.id.action_sponsor:
					if (!(this instanceof SponsorActivity)) {
						Intent myIntent = new Intent(this, SponsorActivity.class);
						this.startActivity(myIntent);
					}
					return true;
			case R.id.action_about:
				AboutDialog ab = new AboutDialog(this);
				ab.show();
				return true;
		}
		return false;
	}

	protected void fnExit() {
		finish();
	}

	@Override
	public void onBackPressed() {
		fnExit();
	}

	protected void toast(String string) {
		Toast t = Toast.makeText(this, string, Toast.LENGTH_LONG);
		t.setGravity(Gravity.BOTTOM, 0, 0);
		t.show();
	}
	
	protected void fnUpdateView() {
//		AppWidgetManager awm = AppWidgetManager.getInstance(this);
//		awm.updateAppWidget(
//				new ComponentName(this, BabyAgeWidgetProvider.class), rv);
		
		String eventName = this.getResources().getString(
				R.string.widget_event_name);
		Intent updateIntent = new Intent(this, BabyAgeWidgetProvider.class);
		updateIntent.setAction(eventName);
		sendBroadcast(updateIntent);
	}
}
